<?php
defined('_JEXEC') or die('Restricted access');
?>
<script type="text/javascript">
$(document).ready(function() {
    if (isNullOrUndef(window.MD5_LOADED)){
        alert('An error on the server: cannot load MD5 library !. Contact Site Administrator');
    } else {
        $('#error_msg').hide();
        $('#create_user_form').submit(function(){
            //var p=$("input[type=text][name=password]").val();
            //$("input[type=text][name=password]").val(CryptoJS.MD5(p));
            $.ajax({
                url: "createSave",
                type: "post",
                data: $('#create_user_form').serialize(),
                // callback handler that will be called on success
                success: function(data, textStatus, jqXHR){
                    //alert(JSON.stringify(data));
                    if (isNullOrUndef(data.response)){
                        alert('Error: No Response from Server, please try again later...')
                    } else {
                        if (data.response=='OK'){
                            //redirect to data.redirect
                            window.location.replace(data.redirect);
                        } else {
                            if (isNullOrUndef(data.error_msg)){
                               alert('The was an internal Server error, please try again later...');
                            } else {
                                // what is the error ? is a form filed missing/not valid?
                                $('#error_msg').html(data.error_msg);
                                $('#error_msg').show();
                            }
                        }
                    }
                },
                // callback handler that will be called on completion, which means, either on success or error
                complete: function(jqXHR, textStatus){},
                error:  function(jqXHR, textStatus, errorThrown){alert('Error: '+JSON.stringify(jqXHR, null, 4));}
            });
            return false;  
        });
    }
});
</script>

<h1>Create User</h1>
<form id="create_user_form" name="create_user_form" method="post" action="<?php echo URL . 'user/createSave'; ?>">
    <label>Username</label><input type="text" name="username" value="" placeholder="Username"/><br />
    <label>Password</label><input type="text" name="password" placeholder="Password"/><br />
    <label>Email</label><input type="text" name="email" value="" placeholder="Email"/><br />
    <input type="hidden" name="form_name" value="<?php echo $this->form_name; ?>" />
    <?php 
      //@TODO: put here hidden input with random hash for security/validiation...
    ?>
    <?php echo '<div id="error_msg" name="error_msg"></div>'; ?>
    <div id="groups_box">
        <?php
        if (!empty($this->groups)) {
            $c = array_keys($this->groups[0]);
            echo '<table><tbody><tr>';
            echo '<th>' . $c[1] . '</th>';
            echo '<th>' . $c[2] . '</th>';
            echo '</tr>';
            
            foreach ($this->groups as $key => $value) {
                echo '<tr>';
                echo '<td><input type="checkbox" name="chk_group[]" value="'.$value['id'].'" />'.$value['name'].'</td>';
                echo '<td>'.$value['description'].'</td>';
                echo '</tr>';
            }
            
            echo '</tbody></table>';
        } else {
            echo "  No Groups found.";
        }
        ?>
    </div>
    <label>&nbsp;</label><input type="submit" />
</form>